﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;

namespace SysManage.Module.TreasureBox
{
    public partial class PartyHistoryTodayEdit : PageBase
    {
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyHistoryToday today = DB.PartyHistoryTodays.Find(id);
                if (today == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtTitle.Text = today.Title;
                ddlMonth.SelectedValue = today.Month.ToString();
                txtDay.Text = today.Day.ToString();
                heContent.Text = today.Content;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;

            if (id != "")
            {
                //更新
                PartyHistoryToday today = DB.PartyHistoryTodays.Find(id);
                if (today == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                today.Title = txtTitle.Text.Trim();
                today.Month = Convert.ToInt32(ddlMonth.SelectedValue);
                today.Day = Convert.ToInt32(txtDay.Text.Trim());
                today.Content = heContent.Text;
                today.ModifyTime = DateTime.Now;
                today.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyHistoryToday today = new PartyHistoryToday();
                today.ID = StringHelper.NewID;
                today.Title = txtTitle.Text.Trim();
                today.Month = Convert.ToInt32(ddlMonth.SelectedValue);
                today.Day = Convert.ToInt32(txtDay.Text.Trim());
                today.Content = heContent.Text;
                today.CreateTime = DateTime.Now;
                today.ModifyTime = DateTime.Now;
                today.CreateBy = CurrentUser.LoginID;
                today.ModifyBy = CurrentUser.LoginID;

                DB.PartyHistoryTodays.Add(today);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            if (!Int32.TryParse(txtDay.Text.Trim(), out int day))
            {
                Alert.Show("日期填写错误，为数字！", String.Empty);
                return;
            }
            if (day < 1 || day > 31)
            {
                Alert.Show("日期填写错误，应在1到31之间！", String.Empty);
                return;
            }
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}